
console.log('background 启动');
import { BackgroundScript } from '../utils/connect';
import { send, connect, closeWebSocketConnection } from './ws';
function changeDevCookies() {
    chrome.cookies.getAll(
        { 'domain': '.xiaoshouyi.com' }
        , function (cookies) {
            const cookie = cookies.filter(cookie => {
                return ['x-ienterprise-passport', 'x-ienterprise-tenant', 'c-user'].indexOf(cookie.name) > -1
            });
            console.log('load cookie:', cookie);
            if (cookie && cookie.length > 0) {
                //获取devhost 刷新cookie
                chrome.storage.sync.get("devHost", ({ devHost }) => {
                    if (!chrome.runtime.error) {
                        if (devHost) {
                            cookie.forEach(c => {
                                chrome.cookies.remove({
                                    url: devHost,
                                    name: c.name
                                }, function () {
                                    chrome.cookies.set({
                                        "url": devHost, "domain":
                                            "127.0.0.1", "name": c.name, "value": c.value, "path": "/"
                                    })
                                    chrome.cookies.set({
                                        "url": devHost, "domain":
                                            "localhost", "name": c.name, "value": c.value, "path": "/"
                                    })
                                    console.log('cookie changed to :', devHost)
                                })

                            })
                        }
                    }
                });
            }
        });
}

/**
 * 监听开发地址变化，更改cookie
 */
chrome.storage.onChanged.addListener(function (changes, namespace) {
    console.log('storage changed')
    for (let [key, { oldValue, newValue }] of Object.entries(changes)) {
        if (key === 'devHost') {
            changeDevCookies();
        }
    }
});




chrome.webNavigation.onCompleted.addListener(function (details) {
    const { url } = details;
    if (~url.indexOf('.xiaoshouyi.com')) {
        console.log('page loaded...');
        changeDevCookies();
    }

});


// chrome.runtime.onMessage.addListener(
//     function (request, sender, sendResponse) {
//         const { type } = request;
//         if (type === "autologin") {
//             const { data,cb } = request;
//             send(JSON.stringify(data));
//         } else if (type === 'connectws') {
//             const { host } = request;
//             connect(host,sendResponse)
//         }else if(type === 'closews'){
//             closeWebSocketConnection();
//         }
//         console.log('request==', request)
//         // Note: Returning true is required here!
//         //  ref: http://stackoverflow.com/questions/20077487/chrome-extension-message-passing-response-not-sent
//         return true;
//     });